//
//  DevChartParamsModel.m
//  RuiHeApp
//
//  Created by Jun on 2022/11/24.
//

#import "DevChartParamsModel.h"
#import "DeviceChartFieldModel.h"
#import "DevChartModel.h"
@interface DevChartParamsModel()

@property (nonatomic ,assign) BOOL isFomrNetwork;

@end
@implementation DevChartParamsModel

+ (NSDictionary *)modelCustomPropertyMapper {
 return @{@"chartList" : @"chartParams",
          @"monthParamsList" : @"chartMonth",
          @"yearParamsList" : @"chartYear"};
 }

+ (NSDictionary *)modelContainerPropertyGenericClass {
    return @{@"formFields" : [DeviceChartFieldModel class]};
 }

//扩展底部数据点结构
- (void)setFormFieldsEx:(NSArray *)formFieldsEx{
    NSMutableArray *list = [NSMutableArray arrayWithArray:formFieldsEx];
    NSMutableArray *array = [NSMutableArray array];
    for (NSInteger i = 0; i<10; i++) {
        [list enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            DeviceChartFieldModel *model = [DeviceChartFieldModel yy_modelWithDictionary:obj];
            model.keyName = [NSString stringWithFormat:model.keyName,i+1];
            model.alias = [NSString stringWithFormat:model.alias,i+1];
            [array addObject:model];
        }];
    }
    _formFieldsEx = array;
}

////设置大类型---处理特殊数据结构
//- (void)setType:(DevChartType)type{
//    _type = type;
//    if ((self.type == ChartType_DCDCIN||self.type == ChartType_DCDCOUT)&&self.formFields){
//        NSMutableArray *arr = [NSMutableArray arrayWithArray:self.formFields];
//        [arr addObjectsFromArray:_formFieldsEx];
//        self.formFields = arr;
//    }
//    
//    if ((self.type == ChartType_DCDCIN||self.type == ChartType_DCDCOUT)&&self.chartList.count>1) {
//        NSMutableArray *array = [NSMutableArray array];
//        DevChartModel *chartModel = [self.chartList lastObject];
//        for (NSInteger i = 0; i<10; i++) {
//            [chartModel.params enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//                DeviceChartFieldModel *model = [DeviceChartFieldModel yy_modelWithDictionary:[obj yy_modelToJSONObject]];
//                model.isShowLine = NO;
//                model.keyName = [NSString stringWithFormat:model.keyName,i+1];
//                model.alias = [NSString stringWithFormat:model.alias,i+1];
//                [array addObject:model];
//            }];
//        }
//        //重新赋值，触发 chartModel的selectArray重新更新一次
//        chartModel.params = array;
//    }
//    
//    
//    if (self.type == ChartType_PACK) {
//        [self.chartList enumerateObjectsUsingBlock:^(id  _Nonnull model, NSUInteger idx, BOOL * _Nonnull stop) {
//            DevChartModel *chartModel = model;
//            NSMutableArray *array = [NSMutableArray array];
//            [chartModel.params enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//                DeviceChartFieldModel *field_model = obj;
////                field_model.nickname = [NSString stringWithFormat:@"P%ld_%@",self.packNum,field_model.nickname];
//                field_model.keyName = [NSString stringWithFormat:@"%@",field_model.keyName];
//                [array addObject:field_model];
//            }];
//            //重新赋值，触发 chartModel的selectArray重新更新一次
//            chartModel.params = array;
//        }];
//        
//        [self.formFields enumerateObjectsUsingBlock:^(DeviceChartFieldModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
////            obj.nickname = [NSString stringWithFormat:@"P%ld_%@",self.packNum,obj.nickname];
//            obj.keyName = [NSString stringWithFormat:@"%@",obj.keyName];
//        }];
//    }
//    
//    if (self.type == ChartType_Master||self.type == ChartType_Inverter||self.type == ChartType_PCS) {
//        [self.chartList enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//            DevChartModel *chartModel = obj;
//            chartModel.monthParams = [NSArray yy_modelArrayWithClass:[DeviceChartFieldModel class] json:self.monthParamsList[idx]];
//            chartModel.yearParams = [NSArray yy_modelArrayWithClass:[DeviceChartFieldModel class] json:self.yearParamsList[idx]];
//        }];
//        NSLog(@"------");
//    }
//}

- (void)setMaxSelectCount:(NSInteger)maxSelectCount
{
    _maxSelectCount = maxSelectCount;
    [self.chartList enumerateObjectsUsingBlock:^(DevChartModel*  _Nonnull chartModel, NSUInteger idx, BOOL * _Nonnull stop) {
        chartModel.maxSelectCount = maxSelectCount;
        chartModel.params = chartModel.params;
    }];
}

//数据曲线
- (void)setChartList:(NSArray *)chartList{
    
    if(self.isFomrNetwork){
        _chartList = chartList;
        return;
    }
    //只留一个曲线
    NSMutableArray *list = [NSMutableArray arrayWithArray:chartList];
    NSMutableArray *arr = [NSMutableArray array];
    [list enumerateObjectsUsingBlock:^(NSArray  *_Nonnull array, NSUInteger idx, BOOL * _Nonnull stop) {
        DevChartModel *chartModel = [[DevChartModel alloc] init];
        chartModel.dayParams = [NSArray yy_modelArrayWithClass:[DeviceChartFieldModel class] json:array];
        //有月曲线就加载
        if(idx<self.monthParamsList.count){
            chartModel.monthParams = [NSArray yy_modelArrayWithClass:[DeviceChartFieldModel class] json:self.monthParamsList[idx]];
        }
        //有年曲线就加载
        if(idx < self.yearParamsList.count){
            chartModel.yearParams = [NSArray yy_modelArrayWithClass:[DeviceChartFieldModel class] json:self.yearParamsList[idx]];
        }

        chartModel.params = [chartModel.dayParams mutableCopy];
        NSDate *date = [NSDate date];
        NSTimeInterval interval = [date timeIntervalSince1970];
        chartModel.curTime = [NSString stringWithFormat:@"%ld",(NSInteger)interval];
        [arr addObject:chartModel];
    }];

    _chartList = arr;
}




//从后台获取的配置
-(void)setNetworkChartList:(NSArray *)chartList
{
    _isFomrNetwork = YES;
    _chartList = chartList;
}



- (void)setMonthParamsList:(NSArray *)monthParamsList{
    _monthParamsList = monthParamsList;
}

- (void)setDayParamsList:(NSArray *)dayParamsList{
    _dayParamsList = dayParamsList;
}


//处理网络返回的底部数据点数据，映射取值
- (void)setFormFieldDataArray:(NSArray *)formFieldDataArray{
    _formFieldDataArray = formFieldDataArray;
    if (_formFieldDataArray.count>0) {//存在数据
        NSDictionary *dic = [_formFieldDataArray firstObject];
        //CGFloat sum = [[array valueForKeyPath:@"@sum.floatValue"] floatValue];
        for (DeviceChartFieldModel *model in self.formFields) {
            NSLog(@"--->name:%@  value:%@",model.keyCode,dic[model.keyCode]);
            id obj = dic[model.keyCode];

            if ([model.keyName isEqualToString:@"I_echgall"]) {//Interver 今日总发电量
                NSArray *array = @[dic[@"I_epv1day"]?dic[@"I_epv1day"]:@(0),
                                   dic[@"I_epv2day"]?dic[@"I_epv2day"]:@(0),
                                   dic[@"I_epv3day"]?dic[@"I_epv3day"]:@(0)];
                CGFloat sum = [[array valueForKeyPath:@"@sum.floatValue"] floatValue];
                model.content = [NSString stringWithFormat:@"%.1f",sum];
                continue;
            }
            
            if (obj) {
                if ([obj isKindOfClass:[NSString class]]) {
                    model.content = obj;
                }else{
                    NSNumber *number = obj;
                    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
                    formatter.numberStyle = NSNumberFormatterDecimalStyle;
                    formatter.maximumFractionDigits = 1;
                    model.content = [formatter stringFromNumber:number];
                }
            }else{
                model.content = @"";
            }
        }
    }else{//未取到数据--
        for (DeviceChartFieldModel *model in self.formFields) {
            model.content = @"";
        }
    }
}
@end
